#coding=utf-8
__author__ = 'rsg'

from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker


class Get_DBSession:

    def __init__(self):
        self.dbsession = None

    def __enter__(self):
        #初始化数据库链接
        engine = create_engine('mysql+mysqldb://root:123456@localhost:3306/test')

        #创建DBSession类型
        session = sessionmaker(bind=engine)
        self.dbsession = session()
        return self.dbsession

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.dbsession.close()